package com.supermarket.kafka.generator;

import java.util.ArrayList;
import java.util.List;

//作用：商品目录管理，初始化并存储超市所有商品信息
//设计亮点：
//使用静态代码块初始化数据，确保全局唯一
//addProducts 方法批量创建同类商品的不同 SKU
//商品 ID 按类别分段设计（如乳制品从 100000 开始）

public class ProductCatalog {
    private static final List<Product> PRODUCTS = new ArrayList<>();

    static {
        // 初始化商品库
        addProducts(ProductCategory.DAIRY, 100000, "Milk", 200, 1.5);
        addProducts(ProductCategory.DAIRY, 100001, "Cheese", 150, 1.2);
        addProducts(ProductCategory.DAIRY, 100002, "Butter", 180, 1.0);

        addProducts(ProductCategory.BEVERAGES, 101000, "Water", 500, 1.0);
        addProducts(ProductCategory.BEVERAGES, 101001, "Orange Juice", 300, 1.3);
        addProducts(ProductCategory.BEVERAGES, 101002, "Cola", 400, 1.6);

        addProducts(ProductCategory.SNACKS, 102000, "Chips", 350, 1.7);
        addProducts(ProductCategory.SNACKS, 102001, "Chocolate", 250, 1.5);

        addProducts(ProductCategory.FRUITS, 103000, "Apple", 400, 1.4);
        addProducts(ProductCategory.FRUITS, 103001, "Banana", 500, 1.3);

        addProducts(ProductCategory.VEGETABLES, 104000, "Carrot", 300, 1.2);
        addProducts(ProductCategory.VEGETABLES, 104001, "Tomato", 350, 1.3);

        addProducts(ProductCategory.CLEANING, 105000, "Detergent", 150, 0.8);
        addProducts(ProductCategory.CLEANING, 105001, "Disinfectant", 120, 0.9);

        addProducts(ProductCategory.PERSONAL_CARE, 106000, "Shampoo", 180, 1.0);
        addProducts(ProductCategory.PERSONAL_CARE, 106001, "Toothpaste", 200, 1.1);
    }

    private static void addProducts(ProductCategory category, int startId, String baseName, int baseStock, double popularity) {
        for (int i = 0; i < 10; i++) {
            PRODUCTS.add(new Product(startId + i, baseName + " " + (i + 1), category, baseStock, popularity));
        }
    }

    // 获取所有商品或随机商品
    public static List<Product> getAllProducts() {
        return PRODUCTS;
    }

    public static Product getRandomProduct() {
        return PRODUCTS.get((int)(Math.random() * PRODUCTS.size()));
    }
}